package lt.marsrutai.mobile.android.activity;

import lt.marsrutai.mobile.android.AppContext;
import lt.marsrutai.mobile.android.R;
import lt.marsrutai.mobile.android.handler.menu.MenuHandler;
import lt.marsrutai.mobile.android.handler.menu.StandartMenuHandler;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.ArrayAdapter;
import android.widget.SpinnerAdapter;
import android.widget.TextView;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.ActionBar.OnNavigationListener;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import com.google.analytics.tracking.android.EasyTracker;

public class BaseActivity extends SherlockFragmentActivity implements OnNavigationListener {

    private static final int ANDROID_3_0_API_11 = 11;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        prepareContext();
        setupActionBar(getSupportActionBar());
        setupView();
    }
    
    @Override
    protected void onStart() {
        super.onStart();        
        EasyTracker.getInstance().activityStart(this);
    }
    
    @Override
    protected void onStop() {
        super.onStop();        
        EasyTracker.getInstance().activityStop(this);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getSupportMenuInflater().inflate(getMenuResource(), menu);
        return super.onCreateOptionsMenu(menu);
    }

    public int getMenuResource() {
        return R.menu.menu;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        Intent intent = getMenuHandler().handle(item, this);
        if (null != intent) {
            startActivity(intent);
        }
        return true;
    }

    public MenuHandler getMenuHandler() {
        return new StandartMenuHandler();
    }

    @Override
    public boolean onNavigationItemSelected(int itemPosition, long itemId) {
        return false;
    }

    public void prepareContext() {
        // Hide title only on pre-honeycomb versions.
        // API >= 11 does not show ActionBar if title is hidden.
        // Source http://blog.perpetumdesign.com/2011/08/strange-case-of-dr-action-and-mr-bar.html
        if (android.os.Build.VERSION.SDK_INT < ANDROID_3_0_API_11) {
            requestWindowFeature(Window.FEATURE_NO_TITLE);
        }

        setTheme(R.style.Theme_Marsrutai);
    }

    protected void setupActionBar(final ActionBar actionBar) {
        if (null != actionBar) {
            actionBar.setListNavigationCallbacks(new DropdownItemAdapter(this, getNavMenuData()), this);
            actionBar.setDisplayHomeAsUpEnabled(true);
            actionBar.setDisplayShowTitleEnabled(false);

            if (actionBar.getNavigationMode() != getNavigationMode()) {
                actionBar.setNavigationMode(getNavigationMode());
            }
        }
    }

    protected int getNavigationMode() {
        return ActionBar.NAVIGATION_MODE_LIST;
    }

    public NavMenuData getNavMenuData() {
        return new NavMenuData();
    }

    public void setupView() {
    }

    public class NavMenuData {

        String[] labels;
        String[] subLabels;
        String scheduleName;
        int scheduleNamePos;
        String transportId;
        String scope;

        public NavMenuData() {
            this(new String[] {});
        }

        public NavMenuData(String[] labels) {
            this(labels, null, -1, null, null);
        }
        
        public NavMenuData(String[] labels, String[] subLabels) {
            this(labels, subLabels, null, -1, null, null);
        }
        
        public NavMenuData(String[] labels, String scheduleName, int scheduleNamePos, String scope, String transportId) {
            this(labels, null, scheduleName, scheduleNamePos, scope, transportId);
        }

        public NavMenuData(String[] labels, String[] subLabels, String scheduleName, int scheduleNamePos, String scope, String transportId) {
            this.labels = labels;
            this.subLabels = subLabels;
            this.scheduleName = scheduleName;
            this.scheduleNamePos = scheduleNamePos;
            this.transportId = transportId;
            this.scope = scope;
        }
    }

    private class DropdownItemAdapter extends ArrayAdapter<String> implements SpinnerAdapter {

        Activity activity;
        NavMenuData navMenuData;

        public DropdownItemAdapter(Activity activity, NavMenuData navMenuData) {
            super(activity, R.layout.spinner_dropdown_item, navMenuData.labels);

            this.activity = activity;
            this.navMenuData = navMenuData;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            LayoutInflater inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            if (convertView == null) {
                convertView = inflater.inflate(R.layout.spinner_dropdown_item, null);
            }

            if (navMenuData.labels.length >= position - 1) {
                ((TextView) convertView.findViewById(android.R.id.text1)).setText(navMenuData.labels[position]);
            }
            
            if (null != navMenuData.subLabels && navMenuData.subLabels.length >= position - 1) {
                TextView subLabel = (TextView) convertView.findViewById(android.R.id.text2);
                subLabel.setVisibility(View.VISIBLE);
                subLabel.setText(navMenuData.subLabels[position]);
            }

            return convertView;
        }

        @Override
        public View getDropDownView(int position, View convertView, ViewGroup parent) {
            LayoutInflater inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            /**
             * Always inflate. Reusing view from parameter causes wrong views
             * retrieved when calling findViewById on Android 4.0.
             */
            convertView = inflater.inflate(R.layout.spinner_item, null);

            if (navMenuData.labels.length > position) {
                TextView title = ((TextView) convertView.findViewById(android.R.id.text1));
                title.setText(navMenuData.labels[position]);
            }

            if (position == navMenuData.scheduleNamePos && null != navMenuData.scheduleName) {
                TextView scheduleName = ((TextView) convertView.findViewById(R.id.schedule_name));
                
                if (!navMenuData.labels[position].equalsIgnoreCase(navMenuData.scheduleName)) {
                    scheduleName.setText(navMenuData.scheduleName);
                }
                
                scheduleName.setBackgroundColor(AppContext.getTransportInfoResolver().resolveTransportColor(navMenuData.scope, navMenuData.transportId));
                scheduleName.setVisibility(View.VISIBLE);
            }

            return convertView;
        }
    }
}
